package com.uraurora.crescent.advanced.tree.impl;

import com.uraurora.crescent.advanced.tree.AbstractSegmentTree;
import com.uraurora.crescent.advanced.tree.ISegmentTree;
import com.google.common.collect.Iterables;
import com.uraurora.crescent.util.SeiIterables;

import java.util.Arrays;
import java.util.Iterator;
import java.util.function.BinaryOperator;

/**
 * 树状数组为了使用lowbit函数，在索引构造上与线段树不同
 * @param <T>
 */
public class BinaryIndexedTree<T> extends AbstractSegmentTree<T> implements ISegmentTree<T> {

    private final T[] data;

    private T[] tree;

    private final BinaryOperator<T> queryMerge;

    private final BinaryOperator<T> updateMerge;

    private final int size;


    public BinaryIndexedTree(Iterable<T> iterable,
                             BinaryOperator<T> updateMerge,
                             BinaryOperator<T> queryMerge,
                             T defaultValue) {
        this.size = Iterables.size(iterable);

        @SuppressWarnings("unchecked")
        T[] data = (T[]) SeiIterables.toArray(iterable, this.size);
        this.data = data;

        this.updateMerge = updateMerge;
        this.queryMerge = queryMerge;
        init(defaultValue);

    }

    public BinaryIndexedTree(T[] objects,
                             BinaryOperator<T> updateMerge,
                             BinaryOperator<T> queryMerge,
                             T defaultValue){
        this.size = objects.length;
        @SuppressWarnings("unchecked")
        T[] data = (T[]) new Object[this.size];
        System.arraycopy(objects, 0, data, 0, objects.length);
        this.data = data;

        this.updateMerge = updateMerge;
        this.queryMerge = queryMerge;
        init(defaultValue);
    }


    @Override
    public boolean update(int index, T value) {
        index++;
        for(;index<=size();index+=lowbit(index)){
            tree[index] = updateMerge.apply(get(index), value);
        }
        return true;
    }

    @Override
    @Deprecated
    public boolean update(int left, int right, T value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public T query(int index, T initValue) {
        return query(index, index, initValue);
    }

    @Override
    public T query(int left, int right, T initValue) {
        return queryMerge.apply(internalQuery(left - 1 , initValue), internalQuery(right, initValue));
    }
    private T internalQuery(int index, T initValue){
        index++;
        T res = initValue;
        for (;index > 0; index-=lowbit(index)) {
            res = updateMerge.apply(res, get(index));
        }
        return res;
    }

    @Override
    public Iterator<T> iterator() {
        return new BinaryIndexedTreeIter(0);
    }

    private class BinaryIndexedTreeIter implements Iterator<T>{

        private int cursor;

        private BinaryIndexedTreeIter(int cursor) {
            this.cursor = cursor;
        }

        @Override
        public boolean hasNext() {
            return cursor < size();
        }

        @Override
        public T next() {
            return get(cursor++);
        }
    }

    @Override
    public int size(){
        return this.size;
    }


    //<editor-fold desc="Private Methods">
    private T getParent(int index){
        return tree[index + lowbit(index)];
    }

    private T getBro(int index){
        return tree[index - lowbit(index)];
    }

    private T get(int index){
        return tree[index];
    }

    private void init(T fillVal){
        @SuppressWarnings("unchecked")
        T[] t = (T[]) new Object[this.size + 1];
        Arrays.fill(t, fillVal);
        this.tree = t;
        for (int i = 0; i < size; i++) {
            update(i, data[i]);
        }
    }

    private static int lowbit(int n){
        return n & (~n+1);
    }
    //</editor-fold>
}
